﻿using System;
using System.Collections.Generic;
using System.Linq;
using CourseManager.Model;
using CourseManager.Model.Data;
using Ext.Net;

namespace CourseManager.View.Contents.Teacher
{
	public partial class CourseInfo : System.Web.UI.Page
	{
		private Model.Teacher teacher;
		private Course currentCourse;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (teacher == null)
			{
				if (Session["UserId"] == null)
				{
					Response.Redirect(Constants.ERROR_PAGE);
				}
				else
				{
					teacher = new Model.Teacher(Session["UserId"].ToString());
					foreach(Course course in teacher.CourseList)
					{
						cbCourse.Items.Add(new ListItem(course.CourseName, course.GetPrimaryKey()));
					}

					if (teacher.CourseList.Count > 0)
					{
						currentCourse = teacher.CourseList[0];
						BindData();
					}
				}
			}
		}

		class Item
		{
			public string studentId;
			public string studentName;
			public double grade;
			public double dayGrade;
			public double examGrade;
			public int? absent;
			public int? absent1;
			public int? absent2;
			public int? absent3;
			public int? absent4;
			public int? absent5;
			public int? absent6;
			public int? absent7;
			public int? absent8;
			public int? absent9;
			public int? absent10;
		}

		private void BindData()
		{
			if (currentCourse == null)
				return;
			var ent = new DBEntity();
			var result = from sc in ent.Tab_SC
			             from s in ent.Tab_Student
			             where sc.courseId.Equals(currentCourse.CourseId) &&
			                   sc.tId.Equals(currentCourse.TId) &&
			                   s.studentId.Equals(sc.studentId)
			             select new 
			                    	{
			                    		studentId = s.studentId,
			                    		studentName = s.name,
			                    		grade = sc.grade,
			                    		dayGrade = sc.dayGrade,
			                    		examGrade = sc.examGrade,
			                    		absent = sc.absent
			                    	};

//			System.Diagnostics.Debug.WriteLine(result.Count());
//			foreach (var item in result.Where(item => item.absent != null))
//			{
//				item.absent1 = (item.absent >> 0 & 1);
//				item.absent2 = (item.absent >> 1 & 1);
//				item.absent3 = (item.absent >> 2 & 1);
//				item.absent4 = (item.absent >> 3 & 1);
//				item.absent5 = (item.absent >> 4 & 1);
//				item.absent6 = (item.absent >> 5 & 1);
//				item.absent7 = (item.absent >> 6 & 1);
//				item.absent8 = (item.absent >> 7 & 1);
//				item.absent9 = (item.absent >> 8 & 1);
//				item.absent10 = (item.absent >> 9 & 1);
//			}
			StudentStore.DataSource = result;
			StudentStore.DataBind();
		}
	}
}